package com.qf.controller;

import cn.hutool.log.Log;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.DtoParameter;
import com.qf.dto.FindTemplateDto;
import com.qf.entity.MallGoodsSpecEntity;
import com.qf.entity.MallGoodsTemplateEntity;
import com.qf.service.IBrandService;
import com.qf.service.IMallGoodsService;
import com.qf.service.ITemplateService;
import com.qf.service.impl.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/tempLate")
@Api(tags = "模板控制器")
public class TemplateController {
    @Autowired
    private ITemplateService service;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private IMallGoodsService goodsService;


    @PostMapping("/findAll")
    @ApiOperation("查询所有品牌和属性")
    public FindTemplateDto findAll(){
        return new FindTemplateDto(goodsService.findAll(),brandService.findAll());
    }

    @PostMapping("/findByPage")
    @ApiOperation("分页查询模板")
    public DtoParameter<MallGoodsTemplateEntity> findPage(@ApiParam("分页条件查询所有信息") @RequestBody DtoParameter<MallGoodsTemplateEntity> parameter) {
        PageHelper.startPage(Integer.parseInt(parameter.getPage()), Integer.parseInt(parameter.getSize()));
        List<MallGoodsTemplateEntity> list = service.findByPage(parameter.getWhere());
        PageInfo<MallGoodsTemplateEntity> mallGoodsTemplateEntityPageInfo = new PageInfo<>(list);
        return new DtoParameter<>(String.valueOf(mallGoodsTemplateEntityPageInfo.getPageNum()), String.valueOf(mallGoodsTemplateEntityPageInfo.getPageSize()), null, list, String.valueOf(mallGoodsTemplateEntityPageInfo.getTotal()));
    }

    @PostMapping("/add")
    @ApiOperation("新增模板")
    public Map<String, String> add(@ApiParam("新增对象所有信息")@RequestBody MallGoodsTemplateEntity entity) {
        if (entity == null) {
            return new HashMap<String, String>() {{
                put("esg", "添加失败");
            }};
        }
        int add = service.add(entity);
        if (add == 1) {
            return new HashMap<String, String>() {{
                put("esg", "添加成功");
            }};
        } else {
            return new HashMap<String, String>() {{
                put("esg", "添加失败");
            }};
        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除模板")
    public Map delete(@ApiParam("删除id") String id) {
        if (id == null && id.equals("")) {
            return new HashMap<String, String>() {{
                put("esg", "删除失败");
            }};
        }
        int delete = service.delete(id);
        if (delete == 1) {
            return new HashMap<String, String>() {{
                put("esg", "删除成功");
            }};
        } else {
            return new HashMap<String, String>() {{
                put("esg", "删除失败");
            }};
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改模板")
    public Map update(@ApiParam("修改模板所有信息")@RequestBody MallGoodsTemplateEntity entity) {
        if (entity.getId() == null && "".equals(entity.getId())) {
            return new HashMap<String, String>() {{
                put("esg", "修改失败");
            }};
        }
        int update = service.update(entity);
        if (update == 1) {
            return new HashMap<String, String>() {{
                put("esg", "修改成功");
            }};
        } else {
            return new HashMap<String, String>() {{
                put("esg", "修改失败");
            }};
        }
    }
}
